package com.tianqu.tms.www.rental.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.RentalContractInfo;

/**
 *  mufeiyu
 */
public class RentalOwnerNameListAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(RentalOwnerNameListAjaxAction.class);
	@Override
	public void process( ActionMapping mapping, ActionForm form
	                                          , HttpServletRequest request
	                                          , HttpServletResponse response
	                                          , UserContext uc)throws TmsException {
		
		logger.debug(this.getClass().getName() + "#execute.");
		
		String query = request.getParameter("query");
		List list = new ArrayList();
		Map map = null;

		RentalContractInfo rental = RentalContractInfo.getInstance();
		List<RentalContractInfo> rentalContractInfoList = rental.findOwnerNameList(query);

		for (RentalContractInfo rentalContractInfo:rentalContractInfoList) {
			map = new HashMap();
			map.put("value", rentalContractInfo.getOwnerName());
			map.put("data", rentalContractInfo.getId());
			list.add(map);
		}

		 JSONArray jsonArray =  JSONArray.fromObject(JSONArray.fromObject(list));

		 JSONObject json =new JSONObject(); 
		 json.put("query", query);
		 json.put("suggestions", jsonArray);
		 
		responseValues(response, json.toString(), TEXT);

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
}
